var models = require('../models'),
	User = models.User;


describe('account', function() {

	function saveUser(callback, done) {
		request(app).post('/account/register').send({
			name: 'miser',
			password: '123456',
			email: 'udmiser@gmail.com'
		}).end(function(err, res) {
			callback(err, res);
			done && done();
		});
	}

	before(function(done) {
		app.listen(0, done);
	});

	beforeEach(function(done) {
		User.remove({}, function() {
			done();
		})
	})
	describe('#register: no user data', function() {
		it('user data wasn‘t passed', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('用户名不得为空，并且不得少于2位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: no password', function() {
		it('password wasn‘t passed', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: 'miser'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('密码不得为空，并且不得少于6位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: no email', function() {
		it('email wasn‘t passed', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: 'miser',
					password: '123456'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('邮箱不得为空，或格式不正确');
					done && done();
				});
			})
		})
	});
	describe('#register: email format', function() {
		it('email format was error', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: 'miser',
					password: '123456',
					email: '11.com'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('邮箱不得为空，或格式不正确');
					done && done();
				});
			})
		})
	});
	describe('#register: password length 1', function() {
		it('password was too short', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: 'miser',
					password: '12345',
					email: 'udmiser@gmail.com'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('密码不得为空，并且不得少于6位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: password length 2', function() {
		it('password was too length', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: 'miser',
					password: '1234567890123',
					email: 'udmiser@gmail.com'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('密码不得为空，并且不得少于6位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: username length 1', function() {
		it('username was too length', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: '1',
					password: '123456',
					email: 'udmiser@gmail.com'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('用户名不得为空，并且不得少于2位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: username length 2', function() {
		it('username was too short', function(done) {
			User.remove({}, function() {
				request(app).post('/account/register').send({
					name: '1234567890123',
					password: '123456',
					email: 'udmiser@gmail.com'
				}).end(function(err, res) {
					var msg = JSON.parse(res.text);
					res.statusCode.should.equal(500);
					msg.message.should.equal('用户名不得为空，并且不得少于2位或长于12位');
					done && done();
				});
			})
		})
	});
	describe('#register: success', function() {
		it('success', function(done) {
			User.remove({}, function() {
				saveUser(function(err, res) {
					res.statusCode.should.equal(200);
				}, done)
			})
		})
	});
	describe('#register: repeat user', function() {
		it('repeat user', function(done) {
			User.remove({}, function() {
				saveUser(function(err, res) {
					res.statusCode.should.equal(200);
					saveUser(function(e, r) {
						r.statusCode.should.equal(500);
						done();
					});
				})
			})
		});
	})
});